package projectwork.shchuangge.cn.com.newproject.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import projectwork.shchuangge.cn.com.newproject.R;
import projectwork.shchuangge.cn.com.newproject.clearedittext.ClearEditText;
import projectwork.shchuangge.cn.com.newproject.clearedittext.PasswordEditText;
import projectwork.shchuangge.cn.com.newproject.projectwork.BasicActivity;


/**
 * Created by 赵航 on 2017/6/15.
 */

public class RegisterActivity extends BasicActivity implements View.OnClickListener {

    //返回的图片
    private ImageView        mBackImage;
    //电话号码
    private ClearEditText    mPhoneNumber;
    //验证码按钮
    private Button           mRegitsterNumber;
    //设置密码按钮
    private PasswordEditText mSetSecrat;

    //开始注册按钮
    private Button mRegitsterButton;

    //已经注册好了
    private TextView mRegsiterOk;
    private Handler mHandler;
    private int time=60;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        //初始化view
        initView();
        //加入数据
        initData();
    }
    private void initView() {

        mBackImage = (ImageView) findViewById(R.id.image_title_left_image);
        mPhoneNumber = (ClearEditText) findViewById(R.id.clear_regsit_phone);
        mRegitsterNumber = (Button) findViewById(R.id.regitster_button_button);
        mSetSecrat = (PasswordEditText) findViewById(R.id.pass_regist_set_password);
        mRegitsterButton = (Button) findViewById(R.id.button_register_regist);
        mRegsiterOk = (TextView) findViewById(R.id.text_register_existingaccount);

    }
    private void initData() {
        mBackImage.setOnClickListener(RegisterActivity.this);
        mRegitsterButton.setOnClickListener(RegisterActivity.this);
        mRegitsterButton.setOnClickListener(RegisterActivity.this);
        mRegsiterOk.setOnClickListener(RegisterActivity.this);
        mRegitsterNumber.setOnClickListener(RegisterActivity.this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            //返回图片
            case R.id.image_title_left_image:

                myStartActivity(LoginActivity.class,true);
                break;
            //验证按钮
            case R.id.regitster_button_button:
                if (mHandler==null) {
                    mHandler = new Handler(Looper.getMainLooper());
                }
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        if (time<=0){

                            mRegitsterNumber.setText("重新发送");
                            //并且关闭倒计时
                            mHandler.removeCallbacksAndMessages(null);

                            return;
                        }

                        mRegitsterNumber.setText(time+"秒后重新发送");
                        time--;
                        mHandler.postDelayed(this, 1000);

                    }
                }, 1000);
                System.out.println("点击了");

                break;
                //开始注册
            case R.id.button_register_regist:
                //TODO:向后台发送请求码进行验证
                if (mHandler!=null) {
                    mHandler.removeCallbacksAndMessages(null);
                }
                myStartActivity(MainActivity.class,true);

                //已有账号
                break;
            case R.id.text_register_existingaccount:
                myStartActivity(LoginActivity.class,true);
                break;
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mHandler!=null) {
            mHandler.removeCallbacksAndMessages(null);
        }
    }
}
